package com.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.hronline.R;
import com.models.Employees;
import com.models.Item;
import com.models.SectionItem;

public class EmployeesAdapter extends ArrayAdapter<Item> {
	
	private Context context;
	private ArrayList<Item> arrData;
	private LayoutInflater inflater;
	
	public EmployeesAdapter(Context context, ArrayList<Item> arrData){
		super(context, 0, arrData);
		
		this.context	=	context;
		this.arrData	=	arrData;
		this.inflater	=	(LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void setArrayData(ArrayList<Item> arrData){
		this.arrData	=	arrData;
	}
	
	public void refreshData(){
		this.arrData.clear();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView	=	convertView;
		
		Item item		=	(Item)this.arrData.get(position);
		
		/* check the item is the section or the item section */
		if(item.isSection()){ 
			
			//this is view for section header
			SectionItem sectionItem = (SectionItem)item;
			rowView = this.inflater.inflate(R.layout.section_header, null);
			
			TextView sectionView = (TextView) rowView.findViewById(R.id.txtHeaderTitle);
			sectionView.setText(sectionItem.getTitle());
		}
		else{
			
			//this is view for item
			Employees empItem	=	(Employees)item;
			
			rowView	=	this.inflater.inflate(R.layout.text_row, null);
			TextView txtContent	=	(TextView)rowView.findViewById(R.id.txtContent);
			
			String empName	=	String.format("%s %s", empItem.getSurName(), empItem.getForeName());
			txtContent.setText(empName);
		}
		
		return rowView;
	}
}
